<?php
class EventHostsController extends AppController {

	var $name = 'EventHosts';

        var $components = array('Acl');

//	function index() {
//		$this->EventHost->recursive = 0;
//		$this->set('eventHosts', $this->paginate());
//	}
//
//	function view($id = null) {
//		if (!$id) {
//			$this->Session->setFlash(__('Invalid event host', true));
//			$this->redirect(array('action' => 'index'));
//		}
//		$this->set('eventHost', $this->EventHost->read(null, $id));
//	}

	function add($id = null) {
                if (!$id) {      
                    $this->Session->setFlash(__('Invalid event', true));
                    //$this->redirect(array('controller'=>'events', 'action' => 'index'));
                }

                $event = $this->EventHost->Event->find('first',array('conditions'=> array('Event.id'=>$id)));

                $host = $this->Session->read('host');

                $alias = $id.'-'.$event['Event']['event_name'];

                //Only the host who created the event and co-hosts can assign co-hosts
                if($this->Acl->check($host['email_addr'],$alias, 'create')){

                    $event_id = $id;
                    $this->set('myevent_id', $event_id);

                    //$this->Event->recursive = 0;
                    $host_id = $host['id'];

                    if (!empty($this->data)) {
                            if(!($this->EventHost->find('count', array('conditions'=> array('EventHost.host_id'=>$host_id, 'EventHost.host_type'=>'p')))>0)){
                                $this->Session->setFlash(__('You dont have permission to assign co-host to this event' , true));
                                $this->redirect(array('controller'=>'events','action' => 'index'));
                            }
                            $this->EventHost->create();
                            //$this->data['EventHost']['host_id'] = $host_id;
                            $this->data['EventHost']['event_id'] = $event_id;
                            $this->data['EventHost']['host_type'] = 's';

                             $this->set('event_host', $this->EventHost->read(null, $id));
                              //   $idd=$event_id;
                            $here = $this->data['EventHost']['host_id'];
                            $already = $this->EventHost->find('count', array
                                ('conditions' => array(
                                    array('eventhost.event_id' => $event_id),
                                    array('eventhost.host_id' => $here))));

                           if ($already <= 0) {

                            if ($this->EventHost->save($this->data)) {
                                    $this->Session->setFlash(__('The co-host has been saved', true));

                                    $coHost = $this->EventHost->Host->find('first',array('conditions'=> array('Host.id'=>$this->data['EventHost']['host_id'])));

                                    //Allow new co-host to invite another co-hosts for this event
                                    $this->Acl->allow($coHost['Host']['email_addr'], $alias,'create');

                                    $this->redirect(array('controller'=>'events','action' => 'index'));
                            } else {
                                    $this->Session->setFlash(__('The co-host could not be saved. Please, try again.', true));
                            }

                           }
                            else{

                                   $this->Session->setFlash('The person is already been asigned as a cohost!');

                            }


                    }
                    $hosts = $this->EventHost->Host->find('list', array('fields'=>array('Host.id', 'Host.email_addr' )
                        ,'conditions'=>array('Host.id <>'=>$host_id)));
                    //$events = $this->EventHost->Event->find('list');
                    $this->set(compact('hosts'));
                    //$this->set('hosts', $hosts);
                    
                }else{
                    $this->Session->setFlash(__('You are not the owner of this event. You cannot invite co-hosts.', true));
                    $this->redirect(array('controller'=>'events','action' => 'index'));
                }
                
	}

//	function edit($id = null) {
//		if (!$id && empty($this->data)) {
//			$this->Session->setFlash(__('Invalid event host', true));
//			$this->redirect(array('action' => 'index'));
//		}
//		if (!empty($this->data)) {
//			if ($this->EventHost->save($this->data)) {
//				$this->Session->setFlash(__('The event host has been saved', true));
//				$this->redirect(array('action' => 'index'));
//			} else {
//				$this->Session->setFlash(__('The event host could not be saved. Please, try again.', true));
//			}
//		}
//		if (empty($this->data)) {
//			$this->data = $this->EventHost->read(null, $id);
//		}
//		$hosts = $this->EventHost->Host->find('list');
//		$events = $this->EventHost->Event->find('list');
//		$this->set(compact('hosts', 'events'));
//	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for event host', true));
			$this->redirect(array('controller'=>'events', 'action'=>'index'));
		}
		if ($this->EventHost->delete($id)) {
			$this->Session->setFlash(__('Event host deleted', true));
			$this->redirect(array('controller'=>'events', 'action'=>'index'));
		}
 	$this->Session->setFlash(__('Event host was not deleted', true));
		$this->redirect(array('controller'=>'events', 'action' => 'index'));
	}
}
?>